/**
 * <strong>Title : EquipmentController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月2日 上午9:54:43<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.b.sky.mes.em.model.EquipmentAttach;
import cn.b.sky.mes.em.model.condition.EquipmentCondition;
import cn.b.sky.mes.em.service.EquipmentAttachService;
import cn.b.sky.mes.em.service.EquipmentService;
import cn.b.sky.mes.em.service.EquipmentStatusService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.b.sky.mes.em.model.Equipment;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

import com.alibaba.fastjson.JSONObject;

/**
 * <strong>Title : EquipmentController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>设备控制器<br> 
 * <strong>Create on : 2015年11月2日 上午9:54:43<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/equipment/*")
public class EquipmentController extends SecurityController {
  @Resource
  EquipmentService equipmentService;

  @Resource
  EquipmentAttachService equipmentAttachService;

  @Resource
  EquipmentStatusService equipmentStatusService;

  @RequestMapping(value = "equipment_list")
  public String equipmentList(String navTabId, Model model, @ModelAttribute("condition") EquipmentCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<Equipment> equipmentList = equipmentService.selectEquipmentList(condition);
    int count = equipmentService.selectEquipmentCount(condition);
    condition.setData(equipmentList);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID,  navTabId);
    return "em/equipment/equipment_list";
  }

  @RequestMapping(value = "addEquipment")
  public String addequipment(String navTabId, Model model, HttpSession session) {
    model.addAttribute("pattern", "add");
    model.addAttribute(NAVTABID,  navTabId);
    model.addAttribute("sessionid", session.getId());
    return "em/equipment/equipment_add";
  }

  @RequestMapping(value = "validateEquipmentCode")
  @ResponseBody
  public boolean validateEquipmentCode(String equipmentCode) {
    if (!StringUtils.isEmpty(equipmentCode)) {
      return null == equipmentService.getEquipment(equipmentCode, getSiteCode());
    }
    return true;
  }

  @RequestMapping(value = "modifyEquipment")
  public String modifyEquipment(String navTabId, Model model, String id, HttpSession session) {
    model.addAttribute("pattern", "modify");
    Equipment equipment = equipmentService.getEquipment(id, getSiteCode());
    List<EquipmentAttach> equipmentAttachList = equipmentAttachService.selectEquipmentAttach(
        equipment.getEquipmentCode(), getSiteCode());
    model.addAttribute(NAVTABID,  navTabId);
    model.addAttribute("id", equipment.getEquipmentCode());
    model.addAttribute("equipment", equipment);
    model.addAttribute("equipmentAttachList", equipmentAttachList);
    model.addAttribute("sessionid", session.getId());
    return "em/equipment/equipment_edit";
  }

  @RequestMapping(value = "viewEquipment")
  public String viewEquipment(String navTabId, Model model, String id) {
    model.addAttribute("pattern", "read");
    Equipment equipment = equipmentService.getEquipment(id, getSiteCode());
    List<EquipmentAttach> equipmentAttachList = equipmentAttachService.selectEquipmentAttach(
        equipment.getEquipmentCode(), getSiteCode());
    model.addAttribute("equipmentAttachList", equipmentAttachList);
    model.addAttribute(NAVTABID,  navTabId);
    model.addAttribute("equipment", equipment);
    return "em/equipment/equipment_view";
  }

  @RequestMapping(value = "saveEquipmentAttachment")
  public void saveEquipmentAttachment(@RequestParam("equipmentFile") MultipartFile equipmentFile,
      HttpServletResponse response) throws IOException {
    response.setContentType("application/json;charset=UTF-8");
    EquipmentAttach equipmentAttach = new EquipmentAttach();
    equipmentAttach.setSiteCode(this.getSiteCode());
    equipmentService.saveEquipmentAttachment(equipmentAttach, equipmentFile);

    response.getWriter().print(JSONObject.toJSONString(ajaxDone(equipmentAttach)));
  }

  @RequestMapping(value = "saveEquipment")
  @ResponseBody
  public DWZResult saveEquipment(Model model, String navTabId, String id, Equipment equipment,
      @RequestParam("pictureAttachment") MultipartFile pictureAttachment, HttpServletRequest request) {
    if (equipment == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    if (StringUtils.isBlank(pictureAttachment.getOriginalFilename())) {

    }
    setCreateInfo(equipment);
    equipment.setSiteCode(getSiteCode());
    if (equipment.getLockFlag() == null) {
      equipment.setLockFlag(0);
    }
    equipmentService.saveOrUpdateEquipment(equipment, id, pictureAttachment);
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping(value = "deleteEquipment")
  @ResponseBody
  public DWZResult deleteEquipment(Model model, String navTabId, Equipment equipment, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] equipmentes = ids.split(",");
    int len = equipmentes.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    int failureCount = 0;
    for (int i = 0; i < equipmentes.length; i++) {
      reports[i] = equipmentService.deleteEquipment(equipmentes[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        failureCount += 1;
      }
    }
    if (failureCount == 0) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }

  }

  @RequestMapping("equipmentLookUp")
  public String equipmentLookUp(Model model, String lookupType,
      @ModelAttribute("condition") EquipmentCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<Equipment> equipmentList = equipmentService.selectEquipmentList(condition);
    int count = equipmentService.selectEquipmentCount(condition);
    condition.setData(equipmentList);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    return "em/equipment/equipment_select";

  }

  @RequestMapping("equipmentSuggest")
  @ResponseBody
  public Object equipmentSuggest(@ModelAttribute("condition") EquipmentCondition equipment) {
    equipment.setSiteCode(this.getSiteCode());
    equipment.setLockFlag(0);
    List<Equipment> equipmentList = equipmentService.selectEquipmentListForSuggest(equipment);
    for (Equipment e : equipmentList) {
      if (StringUtils.isBlank(e.getUseDepartment())) {
        e.setUseDepartment("");
      }
    }
    return equipmentList;
  }

  @RequestMapping(value = "validateEquipmentCodeForSelect")
  @ResponseBody
  public boolean validateEquipmentCodeForSelect(String equipmentCode) {

    if (!StringUtils.isEmpty(equipmentCode)) {
      Equipment equipment = equipmentService.getEquipment(equipmentCode, getSiteCode());
      if (equipment != null) {
        return equipment.getLockFlag() == 0;
      } else {
        return false;
      }
    }
    return false;
  }

}
